package com.cxz.kotlin.baselibs.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cxz.kotlin.baselibs.R;
import com.cxz.kotlin.baselibs.base.MyBaseDialog;


/**
 * 弹窗Dialog
 *
 * @author czg
 * @date 2019年1月19日
 */
public class CustomAlertDialog extends MyBaseDialog {

    private TextView titleTV, contentTV;

    private Button rightBtn;
    private Button leftBtn;

    protected RelativeLayout contentLayout;
    private OnBtnClickListener listener;

    protected LinearLayout rootLayout;

    public CustomAlertDialog(Context context) {
        super(context);
    }

    @Override
    protected int getContentId() {
        return R.layout.dialog_alert_layout;
    }

    @Override
    protected void init() {
        rootLayout = (LinearLayout) this.findViewById(R.id.ll_dialog);
        titleTV = (TextView) this.findViewById(R.id.tv_dialog_title);
        leftBtn = (Button) this.findViewById(R.id.btn_dialog_left);
        rightBtn = (Button) this.findViewById(R.id.btn_dialog_right);
        contentTV = (TextView) findViewById(R.id.tv_dialog_content);
        leftBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onLeftClick(v);
                }
                dismiss();
            }
        });

        rightBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onRightClick(v);
                }
                dismiss();
            }
        });
    }


    public CustomAlertDialog setTitleInfo(CharSequence text) {
        titleTV.setVisibility(View.VISIBLE);
        titleTV.setText(text);
        return this;
    }

    public CustomAlertDialog setTitleInfo(int res) {
        titleTV.setVisibility(View.VISIBLE);
        titleTV.setText(res);
        return this;
    }

    public TextView getTitleTV() {
        return titleTV;
    }

    public TextView getLeftTv() {
        return leftBtn;
    }

    public TextView getRightTv() {
        return rightBtn;
    }


    public CustomAlertDialog setCanceledOutside(boolean isEnable) {
        setCanceledOnTouchOutside(isEnable);
        return this;
    }

    public CustomAlertDialog setOnBtnClickListener(OnBtnClickListener listner) {
        this.listener = listner;
        return this;
    }

    /**
     * 设置左边按钮的文本，默认“取消”
     *
     * @param btnStr
     */
    public CustomAlertDialog setLeftBtnText(String btnStr) {
        leftBtn.setText(btnStr);
        return this;
    }

    public CustomAlertDialog setLeftBtnText(int res) {
        leftBtn.setText(res);
        return this;
    }

    /**
     * 设置右边按钮的文本，默认“确认”
     *
     * @param str
     */
    public CustomAlertDialog setRightBtnText(String str) {
        rightBtn.setText(str);
        return this;
    }

    public CustomAlertDialog setRightBtnText(int res) {
        rightBtn.setText(res);
        return this;
    }


    /**
     * 隐藏标题
     */
    public CustomAlertDialog hideTitle() {
        titleTV.setVisibility(View.GONE);
        return this;
    }

    /**
     * 自定义内容页
     */
    public CustomAlertDialog setCenterView(View view) {
        contentTV.setVisibility(View.GONE);
        contentLayout.addView(view);
        return this;
    }

    /**
     * 设置内容文本 by text
     *
     * @param text
     */
    public CustomAlertDialog setContentText(CharSequence text) {
        contentTV.setText(text);
        return this;
    }

    /**
     * 设置文本向左对齐
     */
    public void setContentTextLeft() {
        contentTV.setGravity(Gravity.LEFT);
    }

    /**
     * 设置文本内容 by id
     *
     * @param res
     * @return
     */
    public CustomAlertDialog setContentText(int res) {
        contentTV.setText(res);
        return this;
    }

    /**
     * 设置内容文本对其方式
     *
     * @param gravity
     */
    public CustomAlertDialog setContentTextGravity(int gravity) {
        contentTV.setGravity(gravity);
        return this;
    }

    public TextView getContentTV() {
        return contentTV;
    }


    public interface OnBtnClickListener {
        void onRightClick(View v);

        void onLeftClick(View v);

    }
}
